package com.caiyouxi.partner.service.impl;

import com.caiyouxi.common.bean.QueryParams;
import com.caiyouxi.common.bean.ResData;
import com.caiyouxi.common.exception.ErrorCode;
import com.caiyouxi.common.service.BaseService;
import com.caiyouxi.partner.mapper.ChannelContractMapper;
import com.caiyouxi.partner.model.ChannelContractBean;
import com.caiyouxi.partner.service.ChannelContractService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Administrator on 2017/11/21.
 */
@Service
public class ChannelContractServiceImpl extends BaseService implements ChannelContractService {

    private Logger logger  =  Logger.getLogger(ChannelContractServiceImpl.class);

    @Autowired
    private ChannelContractMapper channelContractMapper;

    /**
     *获取渠道协议信息
     **/
    @Override
    public ResData get_contract_info(ChannelContractBean bean) {
        String key = "CHANNEL_Contract"+bean.getApp_id()+bean.getChannel_id();
        ChannelContractBean channelContractBean = null;
        if (redisService.exists(key)){
            channelContractBean =  (ChannelContractBean)redisService.get(key);
        }
        /**缓存没有就查数据库*/
        if (StringUtils.isEmpty(channelContractBean)){
            logger.info("查数据库");
            channelContractBean = channelContractMapper.select_channel_contract(bean.getChannel_id());
            /**数据添加到缓存*/
            redisService.set(key,channelContractBean);
        }
        return  success(channelContractBean) ;
    }



    /**
     *获取游戏配置信息列表
     **/
    @Override
    public ResData get_contract_list(ChannelContractBean bean) {
        Integer reids_page_size = Integer.parseInt( redisService.get("sys.pagesize").toString());
        if(null != reids_page_size){
            bean.setPage_size(reids_page_size);
        }
        List<ChannelContractBean> channelContractList= channelContractMapper.get_channel_contract_list(bean);
        int total = channelContractMapper.get_count(bean);
        QueryParams pageList = new QueryParams();
        pageList.setTotal(total);
        pageList.setPage(bean.getPage());
        pageList.setPage_size(bean.getPage_size());
        pageList.setList(channelContractList);
        return success(pageList);
    }

    /**
     *新增渠道协议
     **/
    @Override
    @Transactional
    public ResData add_contract(ChannelContractBean bean) {
        bean.setCreate_time(new Date());
        int flag  = channelContractMapper.add_channel_contract(bean);
        if (flag == 1){
            /**新增数据时增加缓存*/
            String key = "CHANNEL_Contract"+bean.getApp_id()+bean.getChannel_id();
            redisService.set(key,bean);
            return success(null);
        }
        return failed(ErrorCode.SYS_CAIYOUXISDK_ADD_ERROR.getCode(),ErrorCode.SYS_CAIYOUXISDK_ADD_ERROR.getMsg(), null);
    }


    /**
     修改渠道协议
     **/
    @Override
    @Transactional
    public ResData update_contract(ChannelContractBean bean) {
        bean.setUpdate_time(new Date());
        int flag  = channelContractMapper.update_channel_contract(bean);
        if (flag == 1){
            String key = "CHANNEL_Contract"+bean.getApp_id()+bean.getChannel_id();
            /**修改数据清空缓存*/
            redisService.remove(key);
            /**再添加缓存*/
            redisService.set(key,bean);
            return success(null);
        }
        return failed(ErrorCode.SYS_CAIYOUXISDK_UPDATE_ERROR.getCode(),ErrorCode.SYS_CAIYOUXISDK_UPDATE_ERROR.getMsg(), null);

    }

    /**
     *删除渠道协议
     **/
    @Override
    @Transactional
    public ResData delete_contract(ChannelContractBean bean) {
        String key = "CHANNEL_Contract"+bean.getApp_id()+bean.getChannel_id();
        /**修改数据清空缓存*/
        redisService.remove(key);
        channelContractMapper.delete_channel_contract(bean);
        return success(null);
    }

    public ChannelContractBean get_channel_contract_bean(Integer channel_id,Integer app_id){
        HashMap<String,Object> map = new HashMap();
        map.put("channel_id",channel_id);
        map.put("app_id",app_id);
        ChannelContractBean ccb = channelContractMapper.select_channel_contract_on_map(map);
        if(ccb == null){
            map.put("app_id",app_id);
            map.put("channel_id",0);//该字段为0 ，则表示 app_id 对应的游戏的针对所有渠道的默认分成协议
            ccb = channelContractMapper.select_channel_contract_on_map(map);
        }
        if(ccb == null){
            map.put("channel_id",0);
            map.put("app_id",0);//如果为0，则表示该渠道接入我们所有游戏的默认分成协议
            ccb = channelContractMapper.select_channel_contract_on_map(map);
        }
        return ccb;
    }
}
